package cn.service;

import cn.constant.Messages;
import cn.entity.Tag;
import cn.repository.TagRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @author m on 2023/1/31
 * Description:
 */
@Service
public class TagServiceImpl implements TagService{

    private final TagRepository tagRepository;

    @Autowired
    public TagServiceImpl(TagRepository tagRepository) {
        this.tagRepository = tagRepository;
    }


    @Override
    public Tag getTagById(UUID id) {
        return tagRepository.findById(id).orElse(null);
    }

    @Override
    public Tag getTagByName(String name) {
        return tagRepository.findByName(name);
    }

    @Override
    public Tag createTag(Tag tag) {
        return tagRepository.save(tag);
    }

    @Override
    public Messages deleteTag(Tag tag) {
        if (tag == null) {
            return Messages.WRONG_ID;
        }
        tagRepository.delete(tag);
        return Messages.SUCCESS;
    }

    @Override
    public List<Tag> searchTags(String kw) {
        return tagRepository.findByQuery(kw);
    }

    @Override
    public List<Tag> findTags() {
        return tagRepository.findTagByOpenFalse();
    }


}
